package com.sspro.simplertmp;

import android.hardware.Camera;
import android.media.AudioFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import com.sspro.simplertmp.core.AudioProfile;
import com.sspro.simplertmp.core.BasicProfile;
import com.sspro.simplertmp.core.DataCallbackListener;
import com.sspro.simplertmp.core.ErrorHandleListener;
import com.sspro.simplertmp.core.PushClient;
import com.sspro.simplertmp.core.VideoProfile;
import com.sspro.simplertmp.core.utils.Constant;
import com.wbapp.common.jni.OMXVideoJNI;
import java.util.List;

public class MainActivity extends AppCompatActivity implements DataCallbackListener, ErrorHandleListener {

    private SurfaceView surfaceView;
    private Button btnStart, btnEnd;
    private PushClient pushClient;
    private String url = "rtmp://47.105.202.28:1935/live/testbai1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViewStyle();
    }

    private void initViewStyle() {
        // init View
        surfaceView = findViewById(R.id.surface_view);
        btnStart = findViewById(R.id.btn_start);
        btnEnd = findViewById(R.id.btn_end);

        // init Params
        setInitParams();

        // init Click
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pushClient.startRecord();
                OMXVideoJNI.getInstance().setRTMPUrl(url.getBytes(),1);
            }
        });

        btnEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pushClient.stopRecord();
                OMXVideoJNI.getInstance().setRTMPUrl(url.getBytes(),0);
            }
        });
    }

    private void setInitParams() {
        VideoProfile videoProfile = new VideoProfile(Constant.CameraType.BACK, 30, 1280, 720);
        AudioProfile audioProfile = new AudioProfile(Constant.AudioType.AAC, 8000,
                AudioFormat.ENCODING_PCM_16BIT, AudioFormat.CHANNEL_IN_MONO);
        BasicProfile basicProfile = new BasicProfile("47.105.202.28", "rtmp://47.105.202.28:1935/live/testbai1", 17725, 728, 1, 0);
        pushClient = new PushClient(this, surfaceView, audioProfile, videoProfile, basicProfile, this, this);
        pushClient.init();
    }

    @Override
    public void onGetCameraPreviewSizes(List<Camera.Size> supportedPreviewSizes) {

    }

    @Override
    public void onGetCameraPictureSizes(List<Camera.Size> supportedPictureSizes) {

    }

    @Override
    public void onCameraStatusCallback(int status) {

    }

    @Override
    public void onRecorderStatusCallback(boolean status) {

    }

    @Override
    public void onRenderError(int errCode) {

    }

    @Override
    public void onEncodeError(int errCode) {

    }

}